using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MajiangLayoutHelper : MonoBehaviour
{
    static float Majiang_XSpace = -0.67f;
    static float Majiang_YSpace = -0.34f;

    //优先级设为 0，这样会显示在菜单的顶部
    [MenuItem("GameObject/排序", false, 0)]
    static void SortChildren()
    {
        GameObject selectedObject = Selection.activeGameObject;
        if (selectedObject == null)
        {
            Debug.LogWarning("请先选择一个GameObject");
            return;
        }

        // 获取所有子物体
        Transform[] children = new Transform[selectedObject.transform.childCount];
        for (int i = 0; i < selectedObject.transform.childCount; i++)
        {
            children[i] = selectedObject.transform.GetChild(i);
        }

        // 重新排列位置
        for (int i = 0; i < children.Length; i++)
        {
            Vector3 position = children[i].localPosition;
            position.x = i * -Majiang_XSpace; // 在X轴上间隔-0.68个单位
            children[i].localPosition = position;
        }
    }

    [MenuItem("GameObject/牌堆排序", false, 1)]
    static void SortMajiangStack()
    {
        GameObject selectedObject = Selection.activeGameObject;
        if (selectedObject == null)
        {
            Debug.LogWarning("请先选择一个GameObject");
            return;
        }

        // 获取所有子物体
        Transform[] children = new Transform[selectedObject.transform.childCount];
        for (int i = 0; i < selectedObject.transform.childCount; i++)
        {
            children[i] = selectedObject.transform.GetChild(i);
        }

        // 重新排列位置
        for (int i = 0; i < children.Length; i++)
        {
            Vector3 position = children[i].localPosition;
            int col = i / 2; // 计算列号
            int row = i % 2; // 计算行号（0为偶数位，1为奇数位）
            
            position.x = col * Majiang_XSpace; // X轴间隔-0.68单位
            position.y = row * Majiang_YSpace; // Z轴间隔-0.4单位

            // 如果是奇数位，使用对应偶数位的Y轴值
            if (row == 1)
            {
                // 获取对应的偶数位物体
                int evenIndex = i - 1;
                if (evenIndex >= 0)
                {
                    position.z = children[evenIndex].localPosition.z;
                }
            }
            
            children[i].localPosition = position;
        }
    }

    public static void UpdateAllCameraTransforms(
        Transform eastCamera, 
        Transform southCamera, 
        Transform westCamera, 
        Transform northCamera, 
        Transform tableCenter)
    {
        if (eastCamera == null || southCamera == null || westCamera == null || northCamera == null || tableCenter == null)
        {
            Debug.LogError("所有Transform参数都不能为空！");
            return;
        }

        Vector3 tableCenterPosition = tableCenter.position;

        // 1. 计算东风位摄像机相对于桌子中心的初始相对位置和旋转
        Vector3 relativePosEast = eastCamera.position - tableCenterPosition;
        Quaternion initialRotationEast = eastCamera.rotation;

        // 2. 计算并设置南风位摄像机
        Quaternion rotationSouth = Quaternion.AngleAxis(-90, Vector3.up);
        southCamera.position = tableCenterPosition + (rotationSouth * relativePosEast);
        southCamera.rotation = rotationSouth * initialRotationEast;

        // 3. 计算并设置西风位摄像机
        Quaternion rotationWest = Quaternion.AngleAxis(180, Vector3.up);
        westCamera.position = tableCenterPosition + (rotationWest * relativePosEast);
        westCamera.rotation = rotationWest * initialRotationEast;

        // 4. 计算并设置北风位摄像机
        Quaternion rotationNorth = Quaternion.AngleAxis(90, Vector3.up);
        northCamera.position = tableCenterPosition + (rotationNorth * relativePosEast);
        northCamera.rotation = rotationNorth * initialRotationEast;

        Debug.Log("南、西、北风位摄像机位置和旋转已更新。");
    }




    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
